package org.jiang.springbootdemo.demos.web.test;

import java.util.ArrayList;
import java.util.List;

public class CombinationBacktracking {
    public void combine(int n, int k, List<Integer> combination, int start) {
        if (combination.size() == k) {
            System.out.println(combination);
            return;
        }

        for (int i = start; i <= n; i++) {
            combination.add(i);
            combine(n, k, combination, i + 1);
            combination.remove(combination.size() - 1);
        }
    }

    public static void main(String[] args) {
        CombinationBacktracking backtracking = new CombinationBacktracking();
        List<Integer> combination = new ArrayList<>();
        int n = 4; // 集合中的元素个数
        int k = 2; // 组合的大小
        backtracking.combine(n, k, combination, 1);
    }
}
